package cn.edu.buu.news.service.impl;

import cn.edu.buu.news.entity.Case;
import cn.edu.buu.news.mapper.CaseMapper;
import cn.edu.buu.news.service.CaseService;
import cn.edu.buu.news.vo.PageQuery;
import cn.edu.buu.news.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class CaseServiceImpl implements CaseService {
    @Autowired
    private CaseMapper caseMapper;  // 改为CaseMapper

    @Override
    public void add(Case newsCase) {  // 参数类型改为Case
        caseMapper.insert(newsCase);
    }

    @Override
    public void deleteById(Long caseId) {  // 参数名改为caseId
        caseMapper.deleteById(caseId);
    }

    @Override
    public void update(Case newsCase) {  // 参数类型改为Case
        caseMapper.update(newsCase);
    }

    @Override
    @Transactional(readOnly = true)
    public Case findById(Long caseId) {  // 返回类型和参数名改为Case和caseId
        return caseMapper.selectById(caseId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Case> findAll() {  // 返回类型改为List<Case>
        return caseMapper.selectAll();
    }

    @Override
    @Transactional(readOnly = true)
    public PageResult<Case> findByPage(PageQuery query) {  // 分页结果改为Case
        // 校验page和size，避免负数或0
        int page = query.getPage() < 1 ? 1 : query.getPage();
        int size = query.getSize() < 1 ? 10 : query.getSize();
        int offset = (page - 1) * size;

        List<Case> list = caseMapper.selectByPage(query, offset);  // 改为Case类型的列表
        Long total = caseMapper.selectCount(query);
        return new PageResult<>(list, total);
    }

}
